package com.xinsoft.controller.device;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.*;
import com.xinsoft.entity.vo.CusDevTestPointVo;
import com.xinsoft.service.CusDeviceService;
import com.xinsoft.service.CusTestPointService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/cus/testPoint")
public class CusTestPointController {

    @Autowired
    private CusTestPointService cusCusTestPointService;

    @Autowired
    private CusDeviceService cusDeviceService;

    /**
     * 新增测点
     */
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> add(@RequestBody CusTestPointDTO cusTestPointDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(cusTestPointDTO);
        return Response.succeed(cusCusTestPointService.addCusTestPoint(cusTestPointDTO));
    }

    /**
     * 更新
     */
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> edit(@RequestBody  CusTestPointDTO cusTestPointDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(cusTestPointDTO);
        return Response.succeed(cusCusTestPointService.editCusTestPoint(cusTestPointDTO));
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(cusCusTestPointService.getCusTestPoint(id));
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        cusCusTestPointService.del(ids);
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody CusTestPointPageParam cusTestPointPageParam) {
        return Response.succeed(cusCusTestPointService.findPageListByParam(cusTestPointPageParam));
    }

    /**
     * 不分页
     */
    @PostMapping("/list")
    public Response<?> list(@RequestBody CusTestPointPageParam cusTestPointPageParam) {
        return Response.succeed(cusCusTestPointService.findListByParam(cusTestPointPageParam));
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") CusTestPointPageParam cusTestPointPageParam, HttpServletResponse response) throws Exception {
        List<CusDevTestPointVo> cusTestPointList = cusCusTestPointService.findListByParam(cusTestPointPageParam);
        ExcelUtils.exportByEasypoi(cusTestPointList, SysClientDTO.class, "测点信息", "测点信息", response, "测点信息");
        return Response.succeed();
    }

    /**
     * 不分页查询联网设备列表
     */
    @PostMapping("/networkingDeviceList")
    public Response<?> networkingDeviceList(@RequestBody CusDeviceNetWorkDTO cusDeviceNetWorkDTO) {
        return Response.succeed(cusDeviceService.networkingDeviceList(cusDeviceNetWorkDTO));
    }

    /**
     * 导入
     */
    @PostMapping("/import")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Response<?> importExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        return Response.succeed( cusCusTestPointService.importExcel(multipartFile));
    }

}
